
***********************
***********************
**Regresiones básicas**
***********************
***********************
clear
use "$base_out/alum_pb_2018_modified"

drop _merge

merge m:1 school_id using "$base_out/DWMS_Regresiones.dta", keepus(management_sted3 enumerador)

***PEC PLUS VS. PEC****
label var PEC_Plus " "
label var PEC " "
label var management_sted3 " "

ivreghdfe prom_pca  (management_sted3=PEC_Plus)  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato#enumerador) first

reghdfe management_sted3 PEC_Plus   if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
matrix tempm=e(b)
scalar first_stage_effect=tempm[1,1]




eststo clear
foreach x in mat_std leg_std promedio_std prom_pca {

	eststo: ivreghdfe `x'  (management_sted3=PEC_Plus)  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato#enumerador)
	matrix tempm=e(b)
	local tempm=string(tempm[1,1], "%9.2fc")
	local tempm_effect=tempm[1,1]

	file open newfile using "$tables/`x'_coef_IV.tex", write replace
	file write newfile "`tempm'"
	file close newfile
	
	local tempm_effect_IV=tempm[1,1]*`=scalar(first_stage_effect)'
	local tempm_effect_IV=string(`tempm_effect_IV', "%9.2gc")
	file open newfile using "$tables/`x'_coef_IV_Causal.tex", write replace
	file write newfile "`tempm_effect_IV'"
	file close newfile
	
	
	test management_sted3
	if r(p)<0.001 {
		di "peque"
		local tempm ="$<0.001$"
		file open newfile using "$tables/`x'_pvalue_IV.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	if r(p)>0.001 {
		di "grande"
		local tempm=string(r(p), "%9.2fc")
		file open newfile using "$tables/`x'_pvalue_IV.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	}
	
	
	
	
	
}


esttab using "$tables/Resultados_IV.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(management_sted3) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	